<template>
  <div class="check-task">
    <div>
      <eagle-input
        name="CheckTaskName"
        label="任务名称"
        v-model="data.CheckTaskName"
        placeholder="任务名称"
        required
        isInputText
      ></eagle-input>
      
      <eagle-input
        name="CheckTypeName"
        label="检查类型"
        v-model="data.CheckTypeName"
        required
        readonly
        placeholder="请选择检查类型"
      ></eagle-input>

      <eagle-input
        name="ItemTypeName"
        v-if="data.CheckType == 'ZCLX0002'"
        label="专项类型"
        v-model="data.ItemTypeName"
        :islink="true"
        readonly
        placeholder="请选择专项类型"
        @click="chooseItemTypes()"
      ></eagle-input>

      <eagle-input
        title="检查要求"
        v-model="data.CheckRequirement"
        rows="3"
        autosize
        type="textarea"
        placeholder="请输入检查要求"
        multiRow
        isInputText
      ></eagle-input>

      <eagle-input
        name="CheckNames"
        v-model="data.CheckNames"
        label="检查人"
        :autosize="true"
        :islink="true"
        :rows="1"
        type="textarea"
        required
        readonly
        placeholder="请选择"
        @click="userChooseDialogShow = true"
      ></eagle-input>

      <eagle-input
            
        label="检查企业"
        v-model="data.EnterpriseName"
        readonly
        
      
      ></eagle-input>

      <eagle-date
        label="开始时间"
        required
        :date.sync="data.StartDate"
      ></eagle-date>

      <eagle-date
        label="结束时间"
        required
        :date.sync="data.EndDate"
      ></eagle-date>

      <eagle-input
        label="状态"
        :value="'已退回'"
        readonly
      ></eagle-input>

      <eagle-input
            
        label="隐患记录"
        :value="
           data.HiddenDangerCnt 
        "
        readonly
        islink
        @click="goTaskHiddenDanger()"
      ></eagle-input>


      
      <div class="bottom-btns">
        <eagle-button type="primary" class="submit-btn" @click="save(true)">
          保存
        </eagle-button>
      </div>
    </div>

    <!--检查人选择-->
    <user-choose-dialog
      title="选择检查人"
      :show.sync="userChooseDialogShow"
      :tagData="userTagData"
      @callback="userChooseCallback"
      :isMulti="true"
    >
    </user-choose-dialog>


    <!--检查类型-->
    <check-type-dialog
      v-model="show"
      :data="data"
      @close="closeCheckType"
    ></check-type-dialog>


    <!--专项类型-->
    <choose-item-popup
      v-model="ItemTypesShow"
      :list="data.ItemTypes"
      @cb="_chooseItemTypes"
    >
    </choose-item-popup>

  </div>
</template>

<script>
import ChooseItemTypes from "@/components/ChooseItemTypes/ChooseItemTypes.vue";
import UserChooseDialog from "@/views/components/user/userChooseDialog.vue";
import store from "@/store/index";
import checkTypeDialog from "@/views/components/danger/checkTypeDialog.vue";
import chooseItemPopup from "@/views/components/danger/dangerItemPopup.vue";
import dangerTemplateDialog from "./dangerTemplateChooseDialogcopy.vue";

import inspectionWayDialog from "@/views/components/danger/inspectionWayDialog.vue";
import dangerAreaDialog from "@/views/components/danger/dangerAreaDialog.vue";
export default {
  components: {
    ChooseItemTypes,
    UserChooseDialog,
    checkTypeDialog,
    chooseItemPopup,
    dangerTemplateDialog,
    inspectionWayDialog,
    dangerAreaDialog,
   
  },
  data() {
    return {
      data: {
        Advance: 3,
      },
      showCalendar: false,
      idx: "",
      show: false,
      showChooseTask: false,
      TCodes: [],
      TNames: [],
      Details3: [],
      openType : "Edit",
      showAdvance: false,
      chooseDangerAreaShow: false,
      dangerTemplateShow: false,
      selCompanyShow:false,
      userChooseDialogShow: false,
      ItemTypesShow: false,
      inspectionWayShow: false,
      DepartmentShow: false,
      filter: {
        id : this.$route.query.TaskId,
      },
      params: [
        {
          title: "公司检查表",
          Name: "Keywords",
          dataType: "CompanyCMPLList",
        },
        {
          title: "公共检查表",
          Name: "Keywords",
          dataType: "SysCMPLList",
        },
        {
          title: "我的检查表",
          Name: "Keywords",
          dataType: "UserList",
        },
      ],
      listUser: [],
      listCheckTable: [],
      userParams: [
        {
          Name: "ChnName",
          dataType: "List",
          accounted: true,
          userKind: 0,
        },
      ],
      chooseCheckTableUrl: "/DangerTemplate/GetPageData",
      userTagData: [],
      templateTagData: [],
      checkTagData:[],
      ChooseCompanyCodes:[],
    };
  },
  created() {
    if (this.filter.id > 0) {
      store.commit("updateTitle", "编辑");
      this.getCheckPlan();
    } 
  },
  methods: {
    _initListUser(list) {
      this.listUser = list;
    },
    _initListChcekTable(list) {
      this.listCheckTable = list;
    },
    // 获取检查区域
    getPatrolRouteDetail(id) {
      
      let that = this;
      that.$axios
      .get("/DangerPatrolRoute/GetPatrolRouteDetail",{params:{ id: id }})
      .then((res) => {
        
        res.data.forEach((item, index) => {
          that.Details3.forEach((item1, index1) => {
            if (item.CheckAreaCode == item1.CheckAreaCode) {
              that.Details3.splice(index1, 1);
            }
          });
        });
        that.Details3 = that.Details3.concat(res.data);
      });
    },
    // 修改天数
    openAdvance() {
      this.showAdvance = true;
    },
    closeAdvance(type) {
      if (type) {
        this.data.Advance = type;
      }
      this.showAdvance = false;
    },

    // 计划详情
    getCheckPlan() {
      let that = this;
      that.$axios
      .get("/OtherDangerCheckTaskJG/GetDetail",{params:that.filter})
      .then((res) => {
        that.data = res.data;
        if (that.data.Details3 && that.data.Details3.length > 0) {
          for (var m = 0; m < that.data.Details3.length; m++) {
            let item3 = that.data.Details3[m];
            item3.AreaCode = item3.CheckAreaCode;
            item3.AreaName = item3.CheckArea;
            item3.AreaType = item3.CheckAreaType;
          }
          that.Details3 = that.data.Details3;
        } else {
          that.Details3 = [];
        }

        that.TCodes = that.data.TCodes ? that.data.TCodes.split(",") : [];
        that.TNames = that.data.TNames ? that.data.TNames.split(",") : [];
        if (that.TCodes.length > 0) {
          for (var i = 0; i < that.TCodes.length; i++) {
            that.templateTagData.push({
              TCode: that.TCodes[i],
              Title: "",
              code: that.TCodes[i],
              name: "",
            });
          }
          for (var j = 0; j < that.templateTagData.length; j++) {
            that.templateTagData[j].Title = that.templateTagData[j].name =
              that.TNames[j];
          }
        }
        that.ChooseCompanyCodes = that.data.EnterpriseCode ? that.data.EnterpriseCode.split(",") : [];
        that.EnterpriseName = that.data.EnterpriseName ? that.data.EnterpriseName.split(",") : [];
        if (that.ChooseCompanyCodes.length > 0) {
          for (var i = 0; i < that.ChooseCompanyCodes.length; i++) {
            that.checkTagData.push({
              CompanyCode: that.ChooseCompanyCodes[i],
              CompanyName: "",
              code: that.ChooseCompanyCodes[i],
              name: "",
            });
          }
          for (var j = 0; j < that.checkTagData.length; j++) {
            that.checkTagData[j].CompanyName = that.checkTagData[j].name =
              that.EnterpriseName[j];
          }
        }



        if (that.data.CheckCodes) {
          let checkUserNameArr = that.data.CheckCodes.split(",");
          for (var i = 0; i < checkUserNameArr.length; i++) {
            that.userTagData.push({ code: checkUserNameArr[i] });
          }
        }
      });
    },

    // 选择检查类型
    checkTypeName() {
      this.show = true;
    },

    closeCheckType(type) {
      if (type) {
        if (type.indexOf("Custom") >= 0) {
          this.data.CheckTypeName = type.substring(7);
        } else {
          this.data.CheckTypes.forEach((item) => {
            if (item.ID == type) {
              this.data.CheckTypeName = item.Name;
            }
          });
        }
        this.data.CheckType = type;
      }
      this.show = false;
    },

    /* 上移 */
    up(index) {
      if (this.Details3.length > 1 && index !== 0) {
        this.Details3 = this.swapItems(this.Details3, index, index - 1);
      }
    },
    // 下移
    down(index) {
      if (this.Details3.length > 1 && index !== this.Details3.length - 1) {
        this.Details3 = this.swapItems(this.Details3, index, index + 1);
      }
    },
    swapItems(arr, index1, index2) {
      arr[index1] = arr.splice(index2, 1, arr[index1])[0];
      return arr;
    },
    // 删除
    del(index) {
      this.Details3.splice(index, 1);
    },
    // 选择专项类型
    chooseItemTypes() {
      this.ItemTypesShow = true;
    },
    _chooseItemTypes(ItemType) {
      if (ItemType) {
        this.data.ItemType = ItemType;
        this.data.ItemTypes.forEach((item) => {
          if (item.ID == this.data.ItemType) {
            this.data.ItemTypeName = item.Name;
          }
        });
      }
      this.ItemTypesShow = false;
    },

	
	// 检查人回调
    userChooseCallback(userlist) {
      this.userTagData = userlist;
      if (userlist) {
        const code = userlist.map((item) => {
          return item.code;
        });
        const name = userlist.map((item) => {
          return item.name;
        });
        this.data.CheckNames = name.join(",");
        this.data.CheckCodes = code.join(",");
      }
      this.userChooseDialogShow = false;
    },
    // 选择计划重复
    _chooseTask() {
      this.showChooseTask = true;
    },
    closeChooseTask(type) {
      if (type) {
        this.data.CheckFrequency = type;
        this.data.CheckFrequencies.forEach((item) => {
          if (item.ID == type) {
            this.data.CheckFrequencyName = item.Name;
          }
        });
      }
      this.showChooseTask = false;
    },
    
    // 跳转隐患页面
    goTaskHiddenDanger() {
        this.$router.push({
        path: "/FeedSafetyProduction/feedhiddendanger",
        query: {
          ctCode: this.data.CTCode,
          openType : this.openType
        },
      });
    },

    // 	检查类型
    close(type) {
      if (type) {
        if (type.indexOf("Custom") >= 0) {
          this.data.CheckTypeName = type.substring(7);
        } else {
          this.data.CheckTypes.forEach((item) => {
            if (item.ID == type) {
              this.data.CheckTypeName = item.Name;
            }
          });
        }
        this.data.CheckType = type;
      }

      this.show = false;
    },
    save(IsPublish) {
      if (!this.data.CheckTaskName) {
        return this.$toast("请填写任务名称");
      }
      if (!this.data.CheckType) {
        return this.$toast("请选择检查类型");
      }
      if (this.data.CheckType == "ZCLX0002") {
        if (!this.data.ItemType) {
          return this.$toast("请选择专项类型");
        }
      }
      if (!this.data.CheckNames) {
        return this.$toast("请选择检查人");
      }
      this.loading = true;
      let param = {
        ID: this.data.ID,
        SourceID: 0,
        IsPublish: IsPublish,
        CheckTaskName: this.data.CheckTaskName,
        CheckType: this.data.CheckType,
        ItemType: this.data.ItemType,
        CheckArea: this.data.CheckArea,
        CheckRequirement: this.data.CheckRequirement,
        TCodes: this.data.TCodes,
        TNames: this.data.TNames,
        CheckCodes: this.data.CheckCodes,
        CheckNames: this.data.CheckNames,
        StartDate: this.data.StartDate,
        EndDate: this.data.EndDate,
        CheckFrequency: this.data.CheckFrequency,
        Advance: this.data.Advance,
        AdvanceFrequency: this.data.AdvanceFrequency,
        IncludingWeekend: this.data.IncludingWeekend,
        JointInspection: this.data.JointInspection,
        EnterpriseCode : this.data.EnterpriseCode,
        EnterpriseName: this.data.EnterpriseName,
        CType: this.data.CType
      };
      if (this.data.ItemType) {
        param.ItemType = this.data.ItemType;
      }
      param.Details3 = this.Details3;
      let that = this;
      let url = "/ChsDangerNotPorduce/EditTaskSave";
      that.$axios
        .post(url,param)
        .then((res) => {
          that.$toast("保存成功");
          that.loading = false;
          setTimeout(() => {
            that.$router.goRefresh(-1);
          }, 500);
        })
        .catch((err) => {
          console.log("err", err);
        });
    },
    closeItemTypes() {
      this.ItemTypesShow = false;
    },
    inspectionWayCallback(item) {
      this.inspectionWayShow = false;
    },
    getCheckAreaTypeName(item,areaType) {
      
      let areaTypeName = "";
      if (!areaType) {
        return "";
      }
      switch (areaType) {
        case "RiskUnit":
          areaTypeName = "风险位置";
          break;
        case "RiskPoint":
          areaTypeName = "风险点";
          break;
        case "RiskUnitArea":
          areaTypeName = "风险区域";
          break;
        case "RentalPlace":
          areaTypeName = "出租场所";
          break;
        case "UserOrg":
          areaTypeName = "部门/班组";
          break;
        case "Equipment":
          areaTypeName = "设施设备";
          break;
        default:
          "";
          break;
      }
      return areaTypeName;
    },
  },
};
</script>

<style lang="less" scoped>
.check-task {
  background-color: #e8f1f8;
  padding-bottom: 20px;
}
.problem {
  margin-bottom: 10px;
}
.bottom-btns {
  margin-top: 10px;
  padding: 0 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .submit-btn {
    width: 100%;
    height: 35px;
    font-size: 14px;
  }
}

.det_tab_item {
  padding: 10px 0;
  background-color: #fff;
  border-radius: 5px;
  margin-bottom: 10px;

  .det_tab_item_text {
    padding: 0 10px;
  }
}
.operate_det {
  background-color: #fff;
  padding: 10px;
  .operate_list {
    font-size: 12px;
    border: 1px solid #e8f1f8;
    .operate_row_first {
      padding: 10px;
      background-color: #e8f1f8;
      border-bottom: 1px solid #e8f1f8;
      margin-bottom: 10px;
    }
    .operate_row {
      padding: 10px;
      border-bottom: 1px solid #e8f1f8;
      /*margin-bottom: 10px;*/
    }
  }
}
.operation-imgs {
  width: 15px;
  height: 15px;
  vertical-align: middle;
  margin-right: 5px;
}

.risk_item_radio {
  width: 100%;
  background-color: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  // border-bottom: 1px solid #e8f1f8;
}
</style>
